<?php

namespace App\Http\Requests;

use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
 * 病历诊断项目字段验证
 * Class DiagnoseStoreRequest
 * @package App\Http\Requests
 */
class DiagnoseStoreRequest extends FormRequest
{
    protected $diagnose;

    /**
     * Prepare the data for validation.
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->diagnose = app('field')->get('diagnose.cn_diagnose');
    }

    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        //  只有中医是才验证 cn_diagnose字段
        return [
            'record_id'                   => [
                'required', 'integer', 'min:1',
                Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                    $user = \Auth::user();
                    if (!$user->isDoctorsAdmin() && !$user->isAdmin()) {
                        $query->where(['doctor_id' => $user->id]);
                    }
                }),
            ],
            'diagnose'                    => [
                'array',
                Rule::exists('st_disease_dictionary', 'id')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                }),
            ],
            'diagnose.*'                  => 'integer|min:1|distinct',
            'cndiagnoses'                 => 'array',
            'cndiagnoses.*.id'            => 'integer|min:1',
            'cndiagnoses.*.cn_disease_id' => [
                'required', 'integer', 'min:1',
                Rule::exists('st_cn_disease_dictionary', 'id')->whereNull('deleted_at')
            ],
            'cndiagnoses.*.type_id'       => [
                'required', 'integer', 'in:' . implode(',', array_keys($this->diagnose['type']))
            ],
            'cndiagnoses.*.carry_key.*'   => 'in:' . implode(',', array_keys($this->diagnose['carry'])),
            'remark'                      => 'nullable|string|max:255',
        ];
    }

    public function messages()
    {
        return [
            'record_id.required'    => '缺少病历ID',
            'record_id.integer'     => '病历ID 必须是整数。',
            'record_id.min'         => '病历ID 必须大于等于 :min。',
            'record_id.exists'      => '病历 不存在或无修改该病历的权限。',
            'diagnose.exists'       => '您选择了不存在的 诊断名称。',
            'diagnose.*.integer'    => '诊断名称 必须是一个整数。',
            'diagnose.*.min'        => '诊断名称 必须大于等于 :min。',
            'diagnose.*.distinct'   => '重复提交了 诊断名称。',
            'cn_diagnose.*.boolean' => '值类型错误，必须为布尔值。',
            'cn_diagnose.type.in'   => '已选的属性 诊断分型 超出可选范围。',
            'remark.max'            => '备注最大 :max 个字符',
        ];
    }
}
